<template>
  <!-- 国内机票搜素组件 -->
  <div class="search-form">
    <!-- 1.头部tab切换 -->
    <el-row type="flex" class="search-tab">
      <span
        v-for="(item,index) in tabs"
        :key="index"
        @click="handleSearchTab(item,index)"
        :class="{active:index===currentTab}"
      >
        <i :class="item.icon"></i>
        {{item.name}}
      </span>
    </el-row>

    <!-- 2.elm表单组件 -->
    <el-form ref="form" :model="form" class="search-form-content" label-width="80px">
      <!-- 2-1.出发城市 
      trigger-on-focus:是否自动显示下来列表
      fetch-suggestions:自动补全
      highlight-first-item:默认第一个选择高亮-->
      <el-form-item label="出发城市">
        <!-- 自动补全输入 -->
        <el-autocomplete
          v-model="form.departCity"
          :fetch-suggestions="queryDepartSearch"
          :highlight-first-item="true"
          :trigger-on-focus="false"
          placeholder="搜索出发城市"
          @select="handleDepartSelect"
          class="el-autocomplete"
        ></el-autocomplete>
      </el-form-item>

      <!-- 2-2.到达城市
    
      -->
      <el-form-item label="到达城市">
        <el-autocomplete
          v-model="form.destCity"
          :fetch-suggestions="queryDestSearch"
          :trigger-on-focus="false"
          :highlight-first-item="true"
          placeholder="搜索到达城市"
          @select="handleDestSelect"
          class="el-autocomplete"
        ></el-autocomplete>
      </el-form-item>

      <!-- 2-3.出发时间 -->
      <el-form-item label="出发时间">
        <!-- 饿了么日期选择组件 -->
        <el-date-picker
          v-model="form.departDate"
          style="width:100%"
          type="date"
          placeholder="选择日期"
          @change="handleDate"
        ></el-date-picker>
      </el-form-item>

      <!-- 2-4.搜索按钮 -->
      <el-form-item label>
        <el-button style="width:100%" type="primary" icon="el-icon-search" @click="handleSubmit">搜索</el-button>
      </el-form-item>
      <!-- 2-5.转换城市 -->
      <div class="reverse">
        <span @click="handleReverse">换</span>
      </div>
    </el-form>
  </div>
</template>

<script>
//引入moment第三方包
import moment from "moment";
export default {
  //数据
  data() {
    return {
      //栏目数据
      tabs: [
        { icon: "iconfont icondancheng", name: "单程" },
        { icon: "iconfont iconshuangxiang", name: "往返" }
      ],
      //当前选项
      currentTab: 0,
      //表单数据
      form: {
        //出发城市+代码，到达城市+代码，出发时间
        departCity: "",
        departCode: "",
        destCity: "",
        destCode: "",
        departDate: ""
      }
    };
  },
  //方法
  methods: {
    //1.切换栏目
    handleSearchTab(item, index) {
      //没有往返功能，所以默认选项不用更改
      // this.currentTab == index;
      if (index === 1) {
        //如果点击往返栏目，就弹出饿了么的提示框
        this.$confirm("目前暂不支持往返，请使用单程选票！", "提示", {
          confirmButtonText: "确定",
          showCancelButton: true,
          type: "warning"
        });
      }
    },

    //封装城市详情获取
    geiCityList(value) {
      return this.$axios({
        url: "/airs/city",
        params: { name: value }
      }).then(res => {
        console.log(res);

        const cityList = res.data.data.map(city => {
          return {
            //replace：替换
            value: city.name.replace(/市$/, ""),
            code: city.sort
          };
        });
        //过滤没有城市代码的选项
        return cityList.filter(city => {
          return city.code;
        });
      });
    },
    //2.出发城市自动补全,输入框获得焦点触发
    //value:是输入的当前值，cb是回调函数
    queryDepartSearch(value, callback) {
      this.geiCityList(value).then(cityList => {
        console.log(cityList);

        //默认选择第一个的城市代码
        if (cityList.length > 0) {
          this.form.departCode = cityList[0].code;
          callback(cityList);
        } else {
          // this.$message.error("城市名有误");
          callback([{ value: "没有找到这个城市" }]);
        }
      });
    },

    //3.出发城市选中下拉框的选项后触发
    handleDepartSelect(item) {
      //城市代码赋值
      this.form.departCode = item.code;
    },

    //4.到达城市自动补全
    queryDestSearch(value, callback) {
      this.geiCityList(value).then(cityList => {
        callback(cityList);
        //默认选择第一个的城市代码
        if (cityList.length > 0) {
          this.form.destCode = cityList[0].code;
        }
      });
    },
    //5.出发城市选中下拉框的选项后触发
    handleDestSelect(item) {
      //城市代码赋值
      this.form.destCode = item.code;
    },

    //6.出发时间,一选中时间就change触发事件
    handleDate(value) {
      console.log("出发时间:" + value);
      this.form.departDate = moment(value).format("YYYY-MM-DD");
    },
    //7.搜索按钮,提交表单
    handleSubmit() {
      //将用户手动输入的值，如果带上市字的话，就用空字符串替换掉，达到清除的效果
      this.form.departCity = this.form.departCity.replace(/市$/, "");
      this.form.destCity = this.form.destCity.replace(/市$/, "");
      console.log(this.form);

      //将出发城市，到达城市，出发时间存储到vuex
      let history = {
        departCity: this.form.departCity,
        destCity: this.form.destCity,
        departDate: this.form.departDate
      };
      this.$store.commit("history/setHistory", history);

      //跳转到机票列表页
      this.$router.push({
        path: "/air/flights",
        query: this.form
      });
    },
    //8.更换搜索城市
    handleReverse() {
      //变量互换，需要一个中间值存值
      let oldCity = this.form.departCity;
      let oldCode = this.form.departCode;
      this.form.departCity = this.form.destCity;
      this.form.departCode = this.form.destCode;
      this.form.destCity = oldCity;
      this.form.destCode = oldCode;
    }
  }
};
</script>

<style lang="less" scoped>
//容器
.search-form {
  border: 1px solid #ddd;
  border-top: none;
  width: 360px;
  height: 350px;
  box-sizing: border-box;
  //1.头部tab栏
  .search-tab {
    span {
      display: block;
      flex: 1;
      text-align: center;
      height: 48px;
      line-height: 42px;
      border-top: 3px solid #eee;
      background: #eee;
      box-sizing: border-box;
    }
    i {
      margin-right: 5px;
      font-size: 18px;
      //第一个子节点
      &:first-child {
        font-size: 16px;
      }
    }
    .active {
      background: #fff;
      border-top-color: orange;
    }
  }

  //2.elm表单
  .search-form-content {
    position: relative;
    padding: 15px 50px 15px 15px;
    .el-autocomplete {
      width: 100%;
    }

    //转换城市
    .reverse {
      position: absolute;
      top: 35px;
      right: 15px;
      //换字连接的线条设置
      &::after,
      &::before {
        position: absolute;
        left: -35px;
        display: block;
        content: "";
        width: 25px;
        height: 1px;
        background: #ccc;
      }
      &::after {
        top: 0;
      }
      &::before {
        top: 60px;
      }
      span {
        position: absolute;
        top: 20px;
        right: 0;
        display: block;
        font-size: 12px;
        background: #999;
        color: #fff;
        width: 20px;
        height: 20px;
        line-height: 18px;
        text-align: center;
        border-radius: 2px;
        cursor: pointer;
        //换字连接的线条设置
        &::after,
        &::before {
          position: absolute;
          left: 10px;
          display: block;
          content: "";
          width: 1px;
          height: 20px;
          background: #ccc;
        }
        &::after {
          top: -20px;
        }
        &::before {
          top: 20px;
        }
      }
    }
  }
}
</style>